package com.riekoff.particlecubeworld.player;


import cc.creativecomputing.control.CCControl;
import cc.creativecomputing.math.CCMath;
import de.negativrekord.blockworld.camera.CameraSet;
import de.negativrekord.blockworld.camera.CameraSet.CameraDistance;
import de.negativrekord.blockworld.camera.CameraSet.SplitMode;

public class ACameraSetController implements AGlobalTimingProgressListener{
	
	@CCControl(name = "set direct")
	private boolean _cSetDirect = true;
	
	@CCControl(name = "direct_split_mode")
	private SplitMode _cDirectSplitMode = SplitMode.THREE;
	
	@CCControl(name = "direct_distance")
	private CameraDistance _cDirectDistanceMode = CameraDistance.MID_MID_FAR;
	
	@CCControl(name="swap", min=0, max=5)
	private int _cDirectSwap = 0;

	@CCControl(name = "min set change time", min = 1f, max = 20f)
	private float _cMinSetChangeTime = 1f;
	
	@CCControl(name = "max set change time", min = 1f, max = 20f)
	private float _cMaxSetChangeTime = 1f;
	
	@CCControl(name = "three propability", min = 0, max = 1f)
	private float _cThreeProb = 1;
	
	@CCControl(name = "two propability", min = 0, max = 1f)
	private float _cTWOProb = 0;
	
	@CCControl(name = "total propability", min = 0, max = 1f)
	private float _cTotalProb = 0;
	
	@CCControl(name = "close close mid prob", min = 0, max = 1f)
	private float _cCloseCloseMidProb = 1;
	@CCControl(name = "mid mid far prob", min = 0, max = 1f)
	private float _cMidMidFarProb = 1;
	
	private CameraSet _myCameraSet;
	
	private float _myProgress = 0;
	
	public ACameraSetController(CameraSet theCameraSet){
		_myCameraSet = theCameraSet;
	}
	
	private float _myTime = 0;
	private int _mySwap = 0;
	private boolean _myIsFirst = true;
	
	public void update(final float theDeltaTime){
		if(_cSetDirect){
			_myCameraSet.setSplitMode(_cDirectSplitMode);
			_myCameraSet.setCameraDistanceMode(_cDirectDistanceMode);
			_myCameraSet.swapMode(_cDirectSwap);
			return;
		}

		_myTime += theDeltaTime;
		float myChangeTime = CCMath.blend(_cMinSetChangeTime, _cMaxSetChangeTime, _myProgress);
		
		if(_myTime > myChangeTime || _myIsFirst){
			_myIsFirst = false;
			_myTime -= myChangeTime;
			
			float myCloseMidProb = _cCloseCloseMidProb + _cMidMidFarProb;
			float myCloseMidRand = CCMath.random(myCloseMidProb);
			if(myCloseMidRand < _cCloseCloseMidProb){
				_myCameraSet.setCameraDistanceMode(CameraDistance.CLOSE_CLOSE_MID);
			}else{
				_myCameraSet.setCameraDistanceMode(CameraDistance.MID_MID_FAR);
			}
			
			float myProb = _cThreeProb + _cTWOProb + _cTotalProb;
			float myRand = CCMath.random(myProb);
			
			if(myRand < _cThreeProb){
				_myCameraSet.setSplitMode(SplitMode.THREE);
				_myCameraSet.swapMode(_mySwap);
			}else if(myRand < _cThreeProb + _cTWOProb){
				_myCameraSet.setSplitMode(SplitMode.TWO);
				_myCameraSet.swapMode(_mySwap);
			}else {
				_myCameraSet.setSplitMode(SplitMode.TOTAL);
			}
			_mySwap++;
			_mySwap%= 6;
			
		}
	}

	@Override
	public void progress(float theProgress) {
		_myProgress = theProgress;
	}

	@Override
	public void onRestart() {
		_mySwap++;
		_mySwap%= 6;
	}
}
